//
// SoapTrace.Net - A soap message monitoring utility. 
//
// Copyright (C) 2005 Matthew Ward
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Matthew Ward (mrward@users.sourceforge.net)

using NUnit.Framework;
using SoapTrace.Core.Diagnostics;
using SoapTrace.Core.Communication;
using System;
using System.IO;

namespace SoapTrace.Tests.TcpPacketMonitoring
{
	/// <summary>
	/// Tests that a disconnection event is triggered when the server closes
	/// the connection.
	/// </summary>
	[ TestFixture ]
	public class ServerClosedConnectionTestFixture
	{
		TcpPacketMonitor Monitor;
		int DisconnectCount;
		TcpCommunicationEventArgs EventArgsReceived;
		
		/// <summary>
		/// Initialises each test.
		/// </summary>
		[ TestFixtureSetUp ]
		public void Init( )
		{
			DisconnectCount = 0;
			Monitor = new TcpPacketMonitor( "209.171.52.99", 80 );
			Monitor.ClientDisconnected += new TcpCommunicationEventHandler( OnClientDisconnected );
			
			StringReader Reader = new StringReader( GetPacketXml( ) );
			PacketLogEntryCollection LogEntries = PacketLogFile.Load( Reader );
			
			foreach( PacketLogEntry Entry in LogEntries )
			{
				Monitor.ProcessPacket( Entry.Packet );
			}
		}		
		
		/// <summary>
		/// Checks that we received a disconnect event.
		/// </summary>
		[ Test ]
		public void OneDisconnectEventReceived( )
		{
			Assert.AreEqual( 1, DisconnectCount, "Expected one disconnection event." );
		}
		
		/// <summary>
		/// Expect the source of the disconnection to be the server.
		/// </summary>
		[ Test ]
		public void SourceIPAddress( )
		{
			Assert.AreEqual( "209.171.52.99:80", EventArgsReceived.SourceAddress.ToString( ),
			                "Unexpected source IP address." );
		}
		
		/// <summary>
		/// Expect the destination IP address to be the client.
		/// </summary>
		[ Test ]
		public void DestinationIPAddress( )
		{
			Assert.AreEqual( "192.168.1.3:1254", EventArgsReceived.DestinationAddress.ToString( ),
			                "Unexpected destination IP address." );
		}
		
		private void OnClientDisconnected( object sender, TcpCommunicationEventArgs e )
		{
			++DisconnectCount;
			EventArgsReceived = e;
		}
		
		/// <summary>
		/// Gets the xml to be read in by the <see cref="PacketLogFile"/>
		/// class.
		/// </summary>
		private string GetPacketXml( )
		{
			return "<Packets>\r\n" +
				"  <Packet Time=\"2005-02-03 13:59:53.40\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 69 00 11 F5 0B 1F 6A 08 00 45 00 00 30 20 31 40 00 80 06 12 DD C0 A8 01 03 D1 AB 34 63 04 E6 00 50 E0 04 65 3D 00 00 00 00 70 02 FF FF 70 ED 00 00 02 04 05 B4 01 01 04 02 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-02-03 13:59:53.55\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 6A 00 11 F5 0B 1F 69 08 00 45 00 00 30 E0 6A 40 00 66 06 6C A3 D1 AB 34 63 C0 A8 01 03 00 50 04 E6 78 2A 11 14 E0 04 65 3E 70 12 FF FF E7 C5 00 00 02 04 05 8C 01 01 04 02 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-02-03 13:59:53.60\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 69 00 11 F5 0B 1F 6A 08 00 45 00 00 28 20 32 40 00 80 06 12 E4 C0 A8 01 03 D1 AB 34 63 04 E6 00 50 E0 04 65 3E 78 2A 11 15 50 10 FF FF 14 62 00 00 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-02-03 13:59:58.00\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 69 00 11 F5 0B 1F 6A 08 00 45 00 00 2A 20 33 40 00 80 06 12 E1 C0 A8 01 03 D1 AB 34 63 04 E6 00 50 E0 04 65 3E 78 2A 11 15 50 18 FF FF 07 4E 00 00 0D 0A </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-02-03 13:59:58.55\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 6A 00 11 F5 0B 1F 69 08 00 45 00 00 28 E3 1C 40 00 66 06 69 F9 D1 AB 34 63 C0 A8 01 03 00 50 04 E6 78 2A 11 15 E0 04 65 40 50 10 FF FD 14 62 00 00 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-02-03 13:59:58.61\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 69 00 11 F5 0B 1F 6A 08 00 45 00 00 2A 20 34 40 00 80 06 12 E0 C0 A8 01 03 D1 AB 34 63 04 E6 00 50 E0 04 65 40 78 2A 11 15 50 18 FF FF 07 4C 00 00 0D 0A </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-02-03 13:59:59.16\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 6A 00 11 F5 0B 1F 69 08 00 45 00 01 08 E3 45 40 00 66 06 68 F0 D1 AB 34 63 C0 A8 01 03 00 50 04 E6 78 2A 11 15 E0 04 65 42 50 18 FF FB FF 0F 00 00 48 54 54 50 2F 31 2E 31 20 34 30 30 20 42 61 64 20 52 65 71 75 65 73 74 0D 0A 53 65 72 76 65 72 3A 20 4D 69 63 72 6F 73 6F 66 74 2D 49 49 53 2F 35 2E 30 0D 0A 44 61 74 65 3A 20 54 68 75 2C 20 30 33 20 46 65 62 20 32 30 30 35 20 31 33 3A 35 32 3A 33 31 20 47 4D 54 0D 0A 43 6F 6E 74 65 6E 74 2D 54 79 70 65 3A 20 74 65 78 74 2F 68 74 6D 6C 0D 0A 43 6F 6E 74 65 6E 74 2D 4C 65 6E 67 74 68 3A 20 38 37 0D 0A 0D 0A 3C 68 74 6D 6C 3E 3C 68 65 61 64 3E 3C 74 69 74 6C 65 3E 45 72 72 6F 72 3C 2F 74 69 74 6C 65 3E 3C 2F 68 65 61 64 3E 3C 62 6F 64 79 3E 54 68 65 20 70 61 72 61 6D 65 74 65 72 20 69 73 20 69 6E 63 6F 72 72 65 63 74 2E 20 3C 2F 62 6F 64 79 3E 3C 2F 68 74 6D 6C 3E </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-02-03 13:59:59.22\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 6A 00 11 F5 0B 1F 69 08 00 45 00 00 28 E3 46 40 00 66 06 69 CF D1 AB 34 63 C0 A8 01 03 00 50 04 E6 78 2A 11 F5 E0 04 65 42 50 11 FF FB 13 81 00 00 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-02-03 13:59:59.27\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 69 00 11 F5 0B 1F 6A 08 00 45 00 00 28 20 35 40 00 80 06 12 E1 C0 A8 01 03 D1 AB 34 63 04 E6 00 50 E0 04 65 42 78 2A 11 F6 50 10 FF 1F 14 5D 00 00 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-02-03 13:59:59.32\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 69 00 11 F5 0B 1F 6A 08 00 45 00 00 28 20 36 40 00 80 06 12 E0 C0 A8 01 03 D1 AB 34 63 04 E6 00 50 E0 04 65 42 78 2A 11 F6 50 11 FF 1F 14 5C 00 00 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-02-03 13:59:59.37\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 6A 00 11 F5 0B 1F 69 08 00 45 00 00 28 E3 5A 40 00 66 06 69 BB D1 AB 34 63 C0 A8 01 03 00 50 04 E6 78 2A 11 F6 E0 04 65 43 50 10 FF FB 13 80 00 00 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"</Packets>";
		}
	}
}
